

//
//  DBManager.m
//  LoginAndRegisterDemo
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "DBManager.h"
#import "FMDB.h"
#import "UserinfoManager.h"

@implementation DBManager

// 收藏
- (void)collectReadingModel:(LWXHomeListMidel *)dModel html:(NSString *)htmlStr {
    
    [self optenDB];
    [self createTable];
    [self insertModel:dModel Html:htmlStr];

}



// 取消收藏

- (void)cancelCollectAricle:(NSString *)contentID {
    
    [self optenDB];
    [self createTable];
    
    BOOL result = [database executeUpdateWithFormat:@"DELETE FROM shoucang WHERE contentid = %@",contentID];
    
    if (result) {
        NSLog(@"取消成功");
        
        
    }
    
    
    
}


// 查询这个人收藏的所有内容
- (NSArray *)selectAllCollect {
    [self optenDB];
    [self createTable];
    FMResultSet *resultSet = [database executeQueryWithFormat:@"SELECT * FROM shoucang WHERE uid = %@",[UserinfoManager shareManager].uid];

    // 声明一个数组
    NSMutableArray *array = [NSMutableArray new];
    while ([resultSet next]) {
        LWXHomeListMidel *model = [LWXHomeListMidel new];
        //赋值
        model.content = [resultSet stringForColumn:@"content"];
        model.coverimg = [resultSet stringForColumn:@"coverimg"];
        model.ID = [resultSet stringForColumn:@"contentid"];
        model.title = [resultSet stringForColumn:@"title"];
        model.name = [resultSet stringForColumn:@"uname"];
        // 添加到数组里面
        [array addObject:model];
        
    }
    
    return array;
}
// 判断是否收藏过
- (BOOL)hasCollect:(NSString *)contentID {
    BOOL hasCollect = NO;
    [self optenDB];
    [self createTable];
    
    FMResultSet *result = [database executeQueryWithFormat:@"SELECT * FROM shoucang WHERE uid = %@ AND contentid = %@",[UserinfoManager shareManager].uid,contentID];
    while ([result next]) {
        hasCollect = YES;
    }
    
    return hasCollect;
    
}


// 单例

+ (instancetype)shareManager {
    static DBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [DBManager new];
        }
    });
    
    return manager;
    
}

// 数据库指针
 static FMDatabase *database = nil;

// 打开数据库
- (void)optenDB {
    NSString *documentPach = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 数据库路径
    NSString *dbPath = [documentPach stringByAppendingPathComponent:@"PK.sqlite"];
    NSLog(@"%@",dbPath);
    // 打开创建数据库
    database = [FMDatabase databaseWithPath:dbPath];
    if ([database open]) {
        NSLog(@"数据库打开成功");
        
    }
 
}

// 创建表

- (void)createTable {
    // int 做不了自增 必须要用 integer auto 放在 key 的前面
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS shoucang (collectid INTEGER PRIMARY KEY AUTOINCREMENT,contentid TEXT, uid TEXT, title TEXT, content TEXT, coverimg TEXT, uname TEXT, html TEXT)";
    BOOL result = [database executeUpdate:sql];
    if (result) {
        NSLog(@"创建表成功");
    }
}

// 插入数据

- (void)insertModel:(LWXHomeListMidel *)model Html :(NSString *)html {
    
    BOOL result = [database executeUpdateWithFormat:@"INSERT INTO shoucang (contentid, uid, title, content,coverimg, uname, html) VALUES (%@,%@,%@,%@,%@,%@,%@)",model.ID,[UserinfoManager shareManager].uid,model.title,model.content,model.coverimg,model.name,html];
    if (result) {
        NSLog(@"插入数据成功");
    }
    
}





@end
